// 准备一个数组存放属性
var propertyName = new Array('id','img','price','post','describe','storeName','sales','grade');
// 当前页码
var currentPage;
// 总页码
var totalPage;
// 包邮信息
var post = '';
// 描述信息
var describe = '';
// 最低价格
var lowest = '';
// 最高价格
var tallest = '';
// 描述信息默认的数据
var desVal = '';
// 商店名默认的数据
var storeVal = '';
// 销售数量默认的数据
var saleVal = -1;
// 评分默认的数据
var gradeVal = -1;
// 商品编号
var goodsID = -1;
// 销量排序
var orderPri = '';
// 后台url地址
let baseURL = 'http://localhost:8888/';

// 一线城市数组
var firstTierCity = new Array('北京','上海','广州','深圳','杭州','海外','江浙皖','京津冀','港澳台');
// 城市数组
var cityList = new Array('长沙','长春','成都','重庆','大连','东莞','佛山','福州','贵阳','合肥','金华','济南','嘉兴','昆明','宁波','南昌','南京','青岛','泉州','沈阳','苏州','天津','温州','无锡','武汉','西安','厦门','郑州','中山','石家庄','哈尔滨');
// 省份数组
var provinceL = new Array('安徽','福建','甘肃','广东','广西','贵州','海南','河北','河南','湖北','湖南','江苏','江西','吉林','辽宁','宁夏','青海','山东','山西','陕西','云南','四川','西藏','新疆','浙江','澳门','香港','台湾','内蒙古','黑龙江');

// 加载城市信息
function loadCity()
{
	var ulsons = document.getElementsByClassName('ulson');

	for (x = 0; x < ulsons.length; x++)
	{
		if (x == 0)
		{
			for (let i in firstTierCity)
			{
				var li = document.createElement('li');
				li.innerText = firstTierCity[i];
				ulsons[x].appendChild(li);
			}
		}
		else if (x == 1)
		{
			for (let j in cityList)
			{
				var li = document.createElement('li');
				li.innerText = cityList[j];
				ulsons[x].appendChild(li);
			}
		}
		else
		{
			for (let k in provinceL)
			{
				var li = document.createElement('li');
				li.innerText = provinceL[k];
				ulsons[x].appendChild(li);
			}
		}
	}
}

// 当页面加载完的时候需要执行的操作
function finishLoad()
{
  loadCity();
  searchPrice();
	ajaxRequest(1);
}

//用户页面商品ajax请求
function ajaxRequest(pageNum)
{
	$.ajax({

		type: 'get',
		url: baseURL + 'goodsList/frontListByES',
		data: 'pageNum=' + pageNum + '&pageSize=10' + '&post=' + post + '&describe=' + describe + '&lowest=' + lowest + '&tallest=' + tallest,
		success: function (data) {

			var goodsData = data.data;

			var centerDiv4 = document.getElementById('center_div4');
			removeEle(centerDiv4);

			$.each(goodsData.list, function(index, item) {

				centerDiv4.appendChild(createGoods(item));

			});

			totalPage = goodsData.totalPage;

			currentPage = goodsData.pageNum;

			setPageNum();
		}

	});
}

//创建商品
function createGoods(goodsData)
{
	var goodsDiv = document.createElement('div');
	goodsDiv.className = 'goodsDiv';

		var inpHidden = document.createElement('input');
		inpHidden.type = 'hidden';
		inpHidden.value = goodsData.id;
		goodsDiv.appendChild(inpHidden);

		var img = document.createElement('img');
		img.className = 'myImg';
		img.width = 200;
		img.height = 200;
		img.src = baseURL + goodsData.img;
		goodsDiv.appendChild(img);

		var priceDiv = document.createElement('div');
		priceDiv.className = 'priceDiv';
		priceDiv.innerText = '￥' + goodsData.price;
		goodsDiv.appendChild(priceDiv);

		var postDiv = document.createElement('div');
		postDiv.className = 'postDiv';
		postDiv.innerText = goodsData.post == 1 ? '包邮' : '不包邮';
		goodsDiv.appendChild(postDiv);

		var fashionDiv = document.createElement('div');
		fashionDiv.className = 'fashionDiv';
			var a1 = document.createElement('a');
			a1.href = '#';
			a1.innerText = goodsData.describe;
			fashionDiv.appendChild(a1);
		goodsDiv.appendChild(fashionDiv);

		var store = document.createElement('div');
		store.className = 'store';
			var a1 = document.createElement('a');
			a1.href = '#';
			a1.innerText = goodsData.storeName;
			store.appendChild(a1);
		goodsDiv.appendChild(store);

		var pay = document.createElement('div');
		pay.className = 'pay';
		pay.innerText = goodsData.sales + '人付款';
		goodsDiv.appendChild(pay);

		//toFixed(): 保留几位小数
		var service = (Math.random()*5).toFixed(1);
		var sog = (Math.random()*5).toFixed(1);

		var score = document.createElement('div');
		score.className = 'score';
		score.innerText = '如实描述: ' + goodsData.grade.toFixed(1);
			var span = document.createElement('span');
			span.innerText = '';
			score.appendChild(span);
			var scoreSon = document.createElement('div');
			scoreSon.innerText = '服务态度: ' + service;
			scoreSon.style.marginTop = '5px';
			score.appendChild(scoreSon);
			var scoreSon2 = document.createElement('div');
			scoreSon2.innerText = '发货速度: ' + sog;
			scoreSon2.style.marginTop = '5px';
			score.appendChild(scoreSon2);
			score.onmouseover = function(){

				this.style.height = '62px';
				span.innerText = '';
				this.style.color = '#ff4200';
				scoreSon.style.color = '#55ff7f';
				this.onmouseout = function(){

					this.style.height = '16px';
					span.innerText = '';
					this.style.color = '#747474';
					scoreSon.style.color = '#747474';

				}

			}
		goodsDiv.appendChild(score);

	return goodsDiv;
}

//设置用户页面页码
function setPageNum()
{
	$('#pages').text(totalPage);
	$('#selectPage').empty();

	for (i = 1; i <= totalPage; i++)
	{
		$('#selectPage').append($('<option>第' + i + '页</option>').val(i));
	}

	$('#selectPage').val(currentPage);

	if (currentPage == 1)
	{
		document.getElementById('preA').style.filter = 'contrast(50%)';
		$('#firstA').css('filter', 'contrast(50%)');
	}
	if (currentPage > 1)
	{
		document.getElementById('preA').style.filter = 'contrast(100%)';
		$('#firstA').css('filter', 'contrast(100%)');
	}
	if (currentPage == totalPage)
	{
		document.getElementById('nextA').style.filter = 'contrast(50%)';
		$('#lastA').css('filter', 'contrast(50%)');
	}
	if (currentPage < totalPage)
	{
		document.getElementById('nextA').style.filter = 'contrast(100%)';
		$('#lastA').css('filter', 'contrast(100%)');
	}
}

//跳转首页
function firstPage()
{
	if (currentPage > 1)
	{
		ajaxRequest(1);
		document.getElementById('anchorA').click();
	}
}

//跳转上一页
function prePage()
{
	if (currentPage > 1)
	{
		ajaxRequest(--currentPage);
		document.getElementById('anchorA').click();
	}
}

//跳转下一页
function nextPage()
{
	if (currentPage < totalPage)
	{
		ajaxRequest(++currentPage);
		document.getElementById('anchorA').click();
	}
}

//跳转尾页
function lastPage()
{
	if (currentPage < totalPage)
	{
		ajaxRequest(totalPage);
		document.getElementById('anchorA').click();
	}
}

//选择跳转页面
function selectSkip()
{
	ajaxRequest(Number(document.getElementById('selectPage').value));
	document.getElementById('anchorA').click();
}

//搜索商品名称
function searchName()
{
	var mySearch = document.getElementById('mySearch').value;

	if (mySearch != '')
	{
		describe = mySearch;
	}
	else
	{
		describe = '';
	}

	ajaxRequest(1);
}

//通过回车触发搜索
function enterSearch()
{
	if (event.keyCode == 13)
	{
		searchName();
	}
}

//筛选包邮的商品
function screenFreeShipping()
{
	var check1 = document.getElementById('check1');

	if (check1.checked == true)
	{
		post = 1;
	}
	else
	{
		post = '';
	}

	ajaxRequest(1);
}

//检测用户输入的值是否为数字，如果是让用户继续操作，如果不是则清空文本框
function changeNum(thisT)
{
	var numRegExp = /^[0-9]*$/;
	if(!numRegExp.test(thisT.value))
	{
		thisT.value = '';
	}
}

//搜索价格在多少到多少的商品，可以有上限或下限
function searchPrice()
{
	var priceText = document.getElementsByClassName('price');
	var ejectDiv = document.getElementById('ejectDiv');

	for (y = 0; y < priceText.length; y++)
	{
		priceText[y].onfocus = function(){

			ejectDiv.style.display = 'block';
			document.getElementById('closeBtn').onclick = function(){

				var PT2 = priceText[2].value;
				var PT3 = priceText[3].value;

				if (PT2 != '')
				{
					lowest = PT2;
				}
				else
				{
					lowest = '';
				}

				if (PT3 != '')
				{
					tallest = PT3;
				}
				else
				{
					tallest = '';
				}

				priceText[0].value = PT2;
				priceText[1].value = PT3;
				priceText[2].value = '';
				priceText[3].value = '';

				ejectDiv.style.display = 'none';

				ajaxRequest(1);

			}

		}
	}
}

//移除子元素
function removeEle(myElement)
{
	while(myElement.firstChild)
	{
		myElement.removeChild(myElement.firstChild);
	}
}
